package kr.co.sdworld.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.log4j.Logger;


public class GlobalConfig 
{
	private static Logger logger = Logger.getLogger(GlobalConfig.class);
	private static Properties configProperties;
	
	private static Properties getPropObject() 
	{
		if( configProperties == null)
		{
			configProperties = new Properties();
		}
		return configProperties;
	}
	
	public static void load(InputStream is) throws IOException
	{
		if(configProperties != null)
		{
			Properties prop = new Properties();
			prop.load(is);
			Iterator<Entry<Object,Object>> it = prop.entrySet().iterator();
			while( it.hasNext())
			{
				Entry<Object,Object> entry = (Entry<Object,Object>)it.next();
				if( configProperties.containsKey(entry.getKey()))
				{
					Object value = configProperties.getProperty((String)entry.getKey());
					logger.warn(entry.getKey() + " is overwrited. - " + value);
				}
				configProperties.setProperty((String)entry.getKey(), (String)entry.getValue());
			}
		}
		else
		{
			getPropObject().load(is);
		}
		
	}
	
	public static void setProperty( String key, String value)
	{
		getPropObject().setProperty( key, value);
	}
	
	public static String getProperty( String key)
	{
		return getPropObject().getProperty( key);
	}
	
	public static String getProperty( String key, String defaultValue)
	{
		return getPropObject().getProperty( key, defaultValue);
	}
}
